package com.efounder.form.util;

import com.efounder.builder.base.data.ESPRowSet;
import com.efounder.form.DMColComponent;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * roewset 取值
 *
 * @autor yqs
 * @date 2019/4/12 20:15
 **/
public class RowSetValueUtils {


    //   public static String getString(EFRowSet efRowSet, DMColComponent dmColComponent) {
//        if (efRowSet == null || dmColComponent == null) {
//            return null;
//        }
//        // 内部数据集
//        String datasetID = dmColComponent.getDataSetID();
//        // 数据集列标志
//        String dataSetColID = dmColComponent.getDataSetColID();
//        String value = efRowSet.getString(dataSetColID, null);
//        if (value == null) {
//            value = efRowSet.getString(datasetID + "." + dataSetColID, "");
//        }
//        return value;
//    }

    /**
     * 从rowset 取值
     *
     * @param rowSet
     * @param dmColComponent
     * @return
     */
//    public static String getString(ESPRowSet rowSet, DMColComponent dmColComponent) {
//        if (rowSet != null && dmColComponent != null) {
//            Object value = getObject(rowSet, dmColComponent);
//            if (value == null) {
//                value = "";
//            }
//            String COL_ID = dmColComponent.getDataSetColID();
//            String DCT_ID = dmColComponent.getViewDataSetID();
//            String VIEW_COL_ID = dmColComponent.getViewDataSetColID();
//            String VALUE_COL_ID = dmColComponent.getValueDataSetColID();
//
//            // 设置了值数据集ID或没有设置外键字典ID，则直接返回显示的值，
//            if ((VALUE_COL_ID != null && VALUE_COL_ID.trim().length() != 0) ||
//                    (DCT_ID == null || DCT_ID.trim().length() == 0)) {
//                return value.toString();
//            }
//            ESPRowSet viewRowSet = rowSet.getID2RowSet(DCT_ID, COL_ID);
//            if (viewRowSet != null) {
//                return viewRowSet.getString(VIEW_COL_ID, value.toString());
//            }
//            return rowSet.getID2Name(DCT_ID, COL_ID, value.toString());
//        }
//        return null;
//    }
    public static String getString(ESPRowSet rowSet, DMColComponent dmColComponent) {
        if (rowSet != null && dmColComponent != null) {
            Object value = getObject(rowSet, dmColComponent);
            if (value == null) {
                value = "";
            }
            //在表单中 目前VIEW_COL_ID 和 COL_ID值是一样的
            String COL_ID = dmColComponent.getDataSetColID();
            String DCT_ID = dmColComponent.getViewDataSetID();
            String VIEW_COL_ID = dmColComponent.getViewDataSetColID();
            String VALUE_COL_ID = dmColComponent.getValueDataSetColID();

            // 设置了值数据集ID或没有设置外键字典ID，则直接返回显示的值，
            if ((VALUE_COL_ID != null && VALUE_COL_ID.trim().length() != 0) ||
                    (DCT_ID == null || DCT_ID.trim().length() == 0)) {
                return value.toString();
            }
            ESPRowSet viewRowSet = rowSet.getID2RowSet(DCT_ID, COL_ID);
            if (viewRowSet != null) {
                return viewRowSet.getString(VIEW_COL_ID, value.toString());
            }
            return rowSet.getID2Name(DCT_ID, VIEW_COL_ID, value.toString());
        }
        return null;
    }

    /**
     * 向rowset放值
     *
     * @param rowSet
     * @param dmColComponent
     * @return
     */
    public static void putString(String valueString, ESPRowSet rowSet, DMColComponent dmColComponent) {
        putString(valueString, rowSet, dmColComponent, null);
    }

    public static void putString(String valueString, ESPRowSet rowSet, DMColComponent dmColComponent, String codeValue) {
        String COL_ID = dmColComponent.getDataSetColID();
        String DCT_ID = dmColComponent.getViewDataSetID();
        String VIEW_COL_ID = dmColComponent.getViewDataSetColID();
        String VALUE_COL_ID = dmColComponent.getValueDataSetColID();

        // 设置了值数据集ID或没有设置外键字典ID，则
        if ((VALUE_COL_ID != null && VALUE_COL_ID.trim().length() != 0) ||
                (DCT_ID == null || DCT_ID.trim().length() == 0)) {
            putStringByDataSetID(valueString, rowSet, dmColComponent);
        } else {

            ESPRowSet viewRowSet = rowSet.getID2RowSet(DCT_ID, COL_ID);
            if (viewRowSet != null) {
                viewRowSet.putString(VIEW_COL_ID, valueString);

            } else {
                rowSet.setID2Name(DCT_ID, COL_ID, valueString);
            }
            //放入得值是代号，不是真实的值
            putStringByDataSetID(codeValue, rowSet, dmColComponent);
        }
    }


    private static void putStringByDataSetID(String valueString, ESPRowSet rowSet, DMColComponent dmColComponent) {

        //fixme 原始代码使用的getInternalDataSetID ,这里改成使用getDataSetID 不知道什么原因
        //String dataSetID = dmColComponent.getInternalDataSetID();
        String dataSetID = dmColComponent.getDataSetID();
        // 数据集列标志
        String dataSetColID = dmColComponent.getDataSetColID();
        String VALUE_COL_ID = dmColComponent.getValueDataSetColID();
        if (VALUE_COL_ID != null && VALUE_COL_ID.trim().length() != 0) {
            dataSetColID = VALUE_COL_ID;
        }
        Object object;
        if (dmColComponent.getIsUserInternalDataSetID()) {
            object = rowSet.getObject(dataSetID + "." + dataSetColID, null);
            if (object == null) {
//                object = rowSet.getObject(dataSetColID, null);
                rowSet.putObject(dataSetColID, valueString);
            } else {
                rowSet.putObject(dataSetID + "." + dataSetColID, valueString);

            }
        } else {
//            object = rowSet.getObject(dataSetColID, null);
            rowSet.putObject(dataSetColID, valueString);
        }
    }


    /**
     * @param rowSet         ESPRowSet
     * @param dmColComponent DMColComponent
     * @return Object
     */
    public static Object getObject(ESPRowSet rowSet, DMColComponent dmColComponent) {

        //fixme 原始代码使用的getInternalDataSetID ,这里改成使用getDataSetID 不知道什么原因
        //String dataSetID = dmColComponent.getInternalDataSetID();

        String dataSetID = dmColComponent.getDataSetID();
        // 数据集列标志
        String dataSetColID = dmColComponent.getDataSetColID();

        String VALUE_COL_ID = dmColComponent.getValueDataSetColID();
        if (VALUE_COL_ID != null && VALUE_COL_ID.trim().length() != 0) {
            dataSetColID = VALUE_COL_ID;
        }
        Object object;

//        if (dmColComponent.getIsUserInternalDataSetID()==null){
//            dmColComponent.setIsUserInternalDataSetID(true);
//        }
        if (dmColComponent.getIsUserInternalDataSetID() != null && dmColComponent.getIsUserInternalDataSetID()) {
            object = rowSet.getObject(dataSetID + "." + dataSetColID, null);
            if (object == null) {
                object = rowSet.getObject(dataSetColID, null);
            }
        } else {
            object = rowSet.getObject(dataSetColID, null);
        }

        object = processFormat(object, dmColComponent);
        return object;

    }

    /**
     * 格式转换
     *
     * @param object         Object
     * @param dmColComponent DMColComponent
     * @return String
     */
    private static Object processFormat(Object object, DMColComponent dmColComponent) {

        String dateFormat = dmColComponent.getDateFormat();
        if (dateFormat == null || "".equals(dateFormat.trim())) {
            return object;
        }

        dateFormat = dateFormat.trim().replaceAll("Y", "y")
                .replaceAll("m", "M").replaceAll("D", "d");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        //日期类型
        if (object instanceof Date) {
            return simpleDateFormat.format((Date) object);
        } else if (object instanceof Number) {
            //long 类型时间
            if (object.toString().length() >= 11) {
                Date d = new Date(((Number) object).longValue());
                return simpleDateFormat.format(d);
            }
        }
        return object;
    }
}
